#include <QStringListModel>
#include <QMessageBox>

#include "wnd_connection.h"
#include "ui_wnd_connection.h"


WndConnection::WndConnection(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::WndConnection)
{
    ui->setupUi(this);

    rowchanged = false;
    ui->txtProfileName->setFocus();


    ReadConnectionList();
}


int WndConnection::ReadConnectionList() {
    cl.read();
    for (int i; i < cl.profileList.size(); i++) {
        ui->listWidget->addItem(cl.profileList[i].profileName);
    }

    return 0;
}


int WndConnection::WriteConnectionList() {
    cl.write();
    return 0;
}


WndConnection::~WndConnection()
{
    delete ui;
}


// add button
void WndConnection::on_pushButton_clicked()
{
    on_add("*NEW*" + (QVariant::fromValue(cl.profileList.count())).toString());
}


void WndConnection::on_pushButton_4_clicked()
{
    this->wndLogin->setVisible(true);
    close();
}

void WndConnection::on_listWidget_currentRowChanged(int currentRow)
{
//    ui->txtProfileName->clear();
    rowchanged = true;
    ui->txtProfileName->setText(cl.profileList[currentRow].profileName);
    ui->txtDatabaseName->setText(cl.profileList[currentRow].databaseName);
    ui->txtPassword->setText(cl.profileList[currentRow].password);
    ui->txtServerName->setText(cl.profileList[currentRow].serverName);
    ui->txtUserName->setText(cl.profileList[currentRow].userName);
    ui->listWidget->currentItem()->setText(ui->txtProfileName->text());
    rowchanged = false;
}

void WndConnection::on_txtProfileName_textChanged(const QString &arg1)
{
    on_changes(arg1);
}


void WndConnection::on_txtServerName_textChanged(const QString &arg1)
{
    on_changes(arg1);
}

void WndConnection::on_txtUserName_textChanged(const QString &arg1)
{
    on_changes(arg1);
}

void WndConnection::on_txtPassword_textChanged(const QString &arg1)
{
    on_changes(arg1);
}

void WndConnection::on_txtDatabaseName_textChanged(const QString &arg1)
{
    on_changes(arg1);
}


void WndConnection::on_changes(const QString &arg1) {
    ui->pushButton_3->setEnabled(true);

    if (rowchanged || !ui->listWidget->count()) return;

    ConnectionProfile cp = cl.profileList[ui->listWidget->currentRow()];
    cp.profileName = ui->txtProfileName->text();
    cp.databaseName = ui->txtDatabaseName->text();
    cp.password = ui->txtPassword->text();
    cp.serverName = ui->txtServerName->text();
    cp.userName = ui->txtUserName->text();
    cl.profileList[ui->listWidget->currentRow()] = cp;
    ui->listWidget->currentItem()->setText(ui->txtProfileName->text());
}


void WndConnection::on_add(const QString &arg1) {
    ConnectionProfile cp = {
        arg1,
        "127.0.0.1",
        "user",
        "",
        "unicon"
    };

    cl.profileList.append(cp);

    ui->listWidget->addItem("");
    ui->listWidget->setCurrentRow(ui->listWidget->count() - 1);
}

void WndConnection::on_pushButton_3_clicked()
{
    WriteConnectionList();
}

void WndConnection::on_pushButton_5_clicked()
{
    this->wndLogin->setVisible(true);
//    WriteConnectionList();
    close();
}
